/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmBuscarProcesoElectoral.java
 *
 * Created on 19/05/2011, 02:10:33 PM
 */
package verins.GUI;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import verins.BO.EstadoProcesoElectoralBO;
import verins.BO.ProcesoElectoralBO;
import verins.BO.TipoProcesoElectoralBO;
import verins.beans.Estadoprocesoelectoral;
import verins.beans.Procesoelectoral;
import verins.beans.Tipoprocesoelectoral;
import verins.beans.Usuario;

/**
 *
 * @author jpenam
 */
public class frmBusquedaProcesoElectoral extends JDialog {
    List lstProcesos=null;
    public Procesoelectoral proceso =null;
    private Usuario usuario=null;
    JFrame caller=null;
    /** Creates new form frmBuscarProcesoElectoral */
    public frmBusquedaProcesoElectoral(Usuario usuario, JFrame vPadre) {
        super(vPadre,true);
        this.usuario=usuario;
        caller=vPadre;
        initComponents();
        getRootPane().setDefaultButton(btnBuscar);
        txtNombre.requestFocus();
        llenarCombos();
        verins.Utils.Comunes.centrarVentana(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtNombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        calFechaIni = new com.toedter.calendar.JDateChooser();
        calFechaFin = new com.toedter.calendar.JDateChooser();
        cmbTipo = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        cmbEstado = new javax.swing.JComboBox();
        btnBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProcesos = new javax.swing.JTable();
        btnSalir = new javax.swing.JButton();
        btnNuevo = new javax.swing.JButton();
        btnVerDetalle = new javax.swing.JButton();
        lblOcurrencias = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Búsqueda de proceso electoral");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Criterios de búsqueda"));

        txtNombre.setText(" ");

        jLabel2.setText("Fecha Inicio");

        jLabel1.setText("Nombre Proceso");

        jLabel3.setText("Tipo Proceso");

        jLabel5.setText("Fecha Fin");

        calFechaIni.setDateFormatString("dd-MM-yyyy");

        calFechaFin.setDateFormatString("dd-MM-yyyy");

        jLabel4.setText("Estado");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addGap(48, 48, 48)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 437, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(cmbEstado, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(cmbTipo, javax.swing.GroupLayout.Alignment.LEADING, 0, 222, Short.MAX_VALUE))
                                .addGap(98, 98, 98)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel5))
                                .addGap(37, 37, 37)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(calFechaFin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(calFechaIni, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)))))
                    .addComponent(jLabel4))
                .addGap(63, 63, 63))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(cmbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(calFechaIni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(calFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        tblProcesos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Proceso", "Tipo", "fecha Inicio", "Fecha Fin"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblProcesos);
        tblProcesos.getColumnModel().getColumn(0).setResizable(false);
        tblProcesos.getColumnModel().getColumn(1).setResizable(false);
        tblProcesos.getColumnModel().getColumn(2).setResizable(false);
        tblProcesos.getColumnModel().getColumn(3).setResizable(false);

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        btnNuevo.setText("Nuevo");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnVerDetalle.setText("Ver Detalle");
        btnVerDetalle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVerDetalleActionPerformed(evt);
            }
        });

        lblOcurrencias.setText("Se encontraron (0) ocurrencias");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 847, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblOcurrencias)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 538, Short.MAX_VALUE)
                        .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 847, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(234, 234, 234)
                        .addComponent(btnVerDetalle, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 284, Short.MAX_VALUE)
                        .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBuscar)
                    .addComponent(lblOcurrencias))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnVerDetalle)
                    .addComponent(btnSalir)
                    .addComponent(btnNuevo))
                .addContainerGap())
        );

        lblOcurrencias.setVisible(false);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
     public void setearBotonesBusqueda(){
        btnNuevo.setText("Seleccionar");
        btnVerDetalle.setVisible(false);
    }
    
     public void setearEstadoBusq(Estadoprocesoelectoral estado){
         cmbEstado.setSelectedItem(estado);
         cmbEstado.setEnabled(false);
     }
     
    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        // TODO add your handling code here:
        if (validarCampos()){
            proceso = obtenerCriterios();
            lstProcesos = ProcesoElectoralBO.BuscarProceso(proceso);
            llenarTabla();
            verins.Utils.Comunes.setearColumnas(tblProcesos);
            lblOcurrencias.setVisible(lstProcesos!=null &&lstProcesos.size()==0);
        }
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        // TODO add your handling code here:
        if (((JButton)evt.getSource()).getText().equalsIgnoreCase("nuevo"))
             new frmProcesoElectoral(null,null,usuario).setVisible(true);
        else{
            //agregar objeto en la otra ventana
            obtenerProcesoSeleccionado();
            dispose();
        }
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void  obtenerProcesoSeleccionado(){
        if (lstProcesos!=null){
            int fila=tblProcesos.getSelectedRow();
            if (fila>=0){
                proceso=(Procesoelectoral)lstProcesos.get(fila);
            }
        }
    }
    
    private boolean validarCampos(){
    
        if (calFechaFin.getDate()!=null && calFechaIni.getDate()!=null &&
                calFechaIni.getDate().after(calFechaFin.getDate())){
            verins.Utils.Mensajes.alerta("La fecha inicio debe ser menor a la fecha fin");
            return false;
        }
        return true;
    }
    
    private void btnVerDetalleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVerDetalleActionPerformed
        // TODO add your handling code here:
        //partidoPolitico= obtenerCriterios();
        //llenarTabla();
                
         if (lstProcesos!=null){
            int fila=tblProcesos.getSelectedRow();
            if (fila>=0){
                frmProcesoElectoral frm=new  frmProcesoElectoral((Procesoelectoral)lstProcesos.get(fila),null,usuario);
                frm.bloquecamposMostrar();
                frm.setearBotonGuardarEditar("Editar");
                frm.setearBotonSalirCancelar("Salir");
                frm.setVisible(true);
              
            }
        }
    }//GEN-LAST:event_btnVerDetalleActionPerformed

   
    
    public void llenarTabla(){
        SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
        verins.Utils.Comunes.limpiarTabla(tblProcesos);
        DefaultTableModel dtm=(DefaultTableModel)tblProcesos.getModel();
        if (lstProcesos!=null&& lstProcesos.size()>0){
            Iterator it=lstProcesos.iterator();
            while(it.hasNext()){
                Procesoelectoral  p=(Procesoelectoral)it.next();
                Object fila[]=new Object[] {p.getNombre(), p.getIdtipoprocesoelectoral().getDescripcion(),sdf.format(p.getFechainicio()),sdf.format(p.getFechafin())};
                dtm.addRow(fila);
            }
        }
        
    }
    private void llenarCombos(){
        List lista=null;
        lista=TipoProcesoElectoralBO.findAll();
        verins.Utils.Comunes.llenarCombo(cmbTipo, lista);
        cmbTipo.insertItemAt(new Tipoprocesoelectoral(0, "Todos"), 0);
        cmbTipo.setSelectedIndex(0);
        lista=EstadoProcesoElectoralBO.findAll();
        verins.Utils.Comunes.llenarCombo(cmbEstado, lista);
        cmbEstado.insertItemAt(new Estadoprocesoelectoral(0, "Todos","Todos"), 0);
        cmbEstado.setSelectedIndex(1);
    }
      private Procesoelectoral obtenerCriterios()
    {

        Procesoelectoral procesoaux=new Procesoelectoral();

        procesoaux.setNombre(txtNombre.getText().trim());
        procesoaux.setIdtipoprocesoelectoral((Tipoprocesoelectoral)cmbTipo.getSelectedItem());
        procesoaux.setIdestadoprocesoelectoral((Estadoprocesoelectoral)cmbEstado.getSelectedItem());
        procesoaux.setFechainicio(calFechaIni.getDate());
        procesoaux.setFechafin(calFechaFin.getDate());
       
        return procesoaux;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmBusquedaProcesoElectoral(null, null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton btnVerDetalle;
    private com.toedter.calendar.JDateChooser calFechaFin;
    private com.toedter.calendar.JDateChooser calFechaIni;
    private javax.swing.JComboBox cmbEstado;
    private javax.swing.JComboBox cmbTipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblOcurrencias;
    private javax.swing.JTable tblProcesos;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
}
